package com.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.util.DensityUtil;
import com.util.R;
import com.util.TextUtil;
import com.util.ToastUtil;
import com.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by marlon on 2016-05-09.
 */
public class FlowLayoutActivity extends BaseActivity {

    @Bind(R.id.fl_hotSearch_flowLayoutActy)
    FlowLayout fl_hotSearch;
    private List<String> itemList;
    private int pos = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acty_flow_layout);
        ButterKnife.bind(this);
        initData();

    }

    @OnClick({R.id.bt_addItem_flowLayoutActy,
            R.id.bt_compressItem_flowLayoutActy,
            R.id.bt_alignItem_flowLayoutActy,
            R.id.bt_commit_flowLayoutActy})
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.bt_addItem_flowLayoutActy:
                TextView item = initItemView();
                if (item != null) {
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(DensityUtil.dipTOpx(10), 0, DensityUtil.dipTOpx(10), 0);
                    fl_hotSearch.addView(item, lp);
                }
                break;
            case R.id.bt_compressItem_flowLayoutActy:
                fl_hotSearch.relayoutToCompress();
                break;
            case R.id.bt_alignItem_flowLayoutActy:
                fl_hotSearch.relayoutToAlign();
                break;
            case R.id.bt_commit_flowLayoutActy:
                List<String> selectItemList = fl_hotSearch.getSelectItemList();
                if (TextUtil.isValidate(selectItemList)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("您已选择:\n");
                    for (int i = 0; i < selectItemList.size(); i++) {
                        sb.append(selectItemList.get(i)).append("\n");
                    }
                    ToastUtil.showBottomToast(sb.toString());
                } else {
                    ToastUtil.showBottomToast("请选择相关选项");
                }
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        initItemList();
    }

    private void initItemList() {
        itemList = new ArrayList<>();
        itemList.add("美食视频");
        itemList.add("料理制作");
        itemList.add("大胃王");
        itemList.add("黑暗料理");
        itemList.add("吃货");
        itemList.add("萌妹子");
        itemList.add("甜点");
        itemList.add("生肉");
        itemList.add("巧克力");
        itemList.add("深夜报社");
        itemList.add("生活");
        itemList.add("秒包");
        itemList.add("厨艺大师赛");
        itemList.add("街头美食");
        itemList.add("我爱你");
        itemList.add("人人网");
        itemList.add("淘宝");
        itemList.add("百度");
    }

    private TextView initItemView() {
        if (pos == itemList.size()) {
            ToastUtil.showBottomToast("已经达到最大值");
            return null;
        }
        TextView tv = new TextView(this);
        tv.setPadding(DensityUtil.dipTOpx(15), DensityUtil.dipTOpx(5), DensityUtil.dipTOpx(15), DensityUtil.dipTOpx(5));
        tv.setTextColor(Color.parseColor("#000000"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        tv.setText(itemList.get(pos++));
        tv.setGravity(Gravity.CENTER);
        return tv;

    }

}
